﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 小工具合集
{
    public partial class 图片转HEX : Form
    {
        public 图片转HEX()
        {
            InitializeComponent();
        }
        FileStream FileNameStr;
        byte[] FileReadByte;   //读取bin文件缓冲
        long FileLen;
        string fileName = @"C:\Users\hyghy\Desktop\0.jpg";

        private void bchoosefile_Click(object sender, EventArgs e)
        {
            try
            {
                openfile.ShowDialog();//文件读取窗口
                fileName = openfile.FileName;//获取文件名
                tbfilename.Text = fileName;
                FileNameStr = File.OpenRead(fileName);
                FileLen = FileNameStr.Length;
                textBox1.Text = (FileLen / 1024).ToString() + "K";
                FileReadByte = new byte[FileLen];
                //for (int i = 0; i < FileLen; i++)
                //{
                //    FileReadByte[i] = ;
                //}

                string str = "unsigned char const pic[] = {\r\n";
                int cnt = 0;
                for (int i = 0; i < FileLen; i++)
                {
                    str += "0x";
                    str += ((byte)FileNameStr.ReadByte()).ToString("X2");
                    str += ", ";
                    cnt++;
                    if (cnt % 10 == 0)
                    {
                        str += "\r\n";
                    }
                }

                textBox2.Text = str + "};\r\n";
            }
            catch (Exception Err)
            {
                MessageBox.Show(Err.Message);
            }
            FileNameStr.Close();
        }
    }
}
